package com.jiang.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiang.bean.CartVo;
import com.jiang.bean.QueryInfo;
import com.jiang.bean.User;
import com.jiang.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;

@CrossOrigin
@RestController
public class UserController {

    @Autowired
    UserService userService;

    @RequestMapping("/alluser1")
    public IPage<User> selectPage(@RequestParam("pageNum") Integer pageNum,
                                  @RequestParam("pageSize") Integer pageSize) {
        /**
         * Page(current,size)
         * current:当前页,long类型
         * size:每页显示的数量,long类型
         * 可参考其构造方法
         */
        CartVo cartVo = new CartVo();
        IPage<User> userIPage = userService.selectPage(new Page<>(pageNum, pageSize));
        return userIPage;
    }
    @CrossOrigin
    @RequestMapping("/allUser")
    public String getUserList(QueryInfo queryInfo){
        System.out.println(queryInfo);
        int numbers = userService.getUserCounts("%" + queryInfo.getQuery() + "%");//获取数据总数
        int pageStart = (queryInfo.getPageNum()-1)*queryInfo.getPageSize();
        List<User> users = userService.getAllUser("%" + queryInfo.getQuery() + "%", pageStart, queryInfo.getPageSize());
        HashMap<String, Object> res = new HashMap<>();
        res.put("numbers",numbers);
        res.put("data",users);
        System.out.println("总条数：" + numbers);
        String s = JSON.toJSONString(res);
        System.out.println(s);
        return s;
    }

    @RequestMapping("/userState")
    public String updateUserState(@RequestParam("id") Integer id,
                                  @RequestParam("state") Boolean state) {
        int i = userService.updateState(id, state);
        System.out.println("用户编号:" + id);
        System.out.println("用户状态:" + state);
        String str = i > 0 ? "success" : "error";
        return str;
    }

    @RequestMapping("/addUser")
    public String addUser(@RequestBody User user) {
        System.out.println(user);
        String username = user.getUsername();
        int total = userService.selectcount(username);
        if(total>=1){
            return "fail";
        }else {
            user.setRole("普通用户");
            user.setState(false);
            int i = userService.addUser(user);
            String str = i > 0 ? "success" : "error";
            return str;
        }

    }


    @RequestMapping("/getUpdate")
    public String getUpdateUser(int id) {
        User updateUser = userService.getUpdateUser(id);
        String users_json = JSON.toJSONString(updateUser);
        return users_json;
    }

    @RequestMapping("/editUser")
    public String editUser(@RequestBody User user) {
        int i = userService.editUser(user);
        String str = i > 0 ? "success" : "error";
        return str;
    }

    @RequestMapping("/deleteUser")
    public String deleteUser(int id) {
        int i = userService.deleteUser(id);
        String str = i > 0 ? "success" : "error";
        return str;
    }
}
